package aishua.com.mytestapplist.webkit;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by aishua on 2018/3/26.
 */

public class SharpJS {
     private Context mContext;
     private WebView webView;
     private Activity activity;

    public  SharpJS(Context mContext,WebView webView){
        this.mContext =mContext;
        this.webView = webView;
        this.activity = (Activity) mContext;
    }
    @JavascriptInterface
    public void contactlist( ) {
        try {
            System.out.println("contactlist()方法执行了！"+"Thread.name:"+Thread.currentThread().getName());
            final String json = buildJson(getContacts());
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    webView.loadUrl("javascript:show('" + json + "')");
                }
            });
        } catch (Exception e) {
            System.out.println("设置数据失败" + e);
        }
    }
    @JavascriptInterface
    public void call(String phone) {
        System.out.println("call()方法执行了！");
        Intent it = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
        mContext.startActivity(it);
    }

    //将获取到的联系人集合写入到JsonObject对象中,再添加到JsonArray数组中
    public String buildJson(List<Contacts> contacts) {
        JSONArray array = new JSONArray();
        for(Contacts contact:contacts)
        {
            JSONObject jsonObject = new JSONObject();

            try {
                jsonObject.put("id", contact.getId());
                jsonObject.put("name", contact.getName());
                jsonObject.put("phone", contact.getPhone());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            array.put(jsonObject);
        }
        return array.toString();
    }



    //定义一个获取联系人的方法,返回的是List<Contact>的数据
    public List<Contacts> getContacts()
    {
        List<Contacts> ContactsList = new ArrayList<Contacts>();
        //①查询raw_contacts表获得联系人的id
        ContentResolver resolver = mContext.getContentResolver();
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        //查询联系人数据
        Cursor cursor = resolver.query(uri, null, null, null, null);
        while(cursor.moveToNext())
        {
            Contacts contact = new Contacts();
            //获取联系人姓名,手机号码
            contact.setId(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
            contact.setName(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
            contact.setPhone(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
            ContactsList.add(contact);
        }
        cursor.close();
        return ContactsList;
    }
}
